# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=osgworks
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
_ver_base=3.1.0
pkgver=3.1.0.444
pkgrel=6
pkgdesc="A toolkit for OpenSceneGraph applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('spdx:LGPL-2.1')
url="https://github.com/mccdo/osgworks/"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
depends=("${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
         "${MINGW_PACKAGE_PREFIX}-vrpn")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "git")
_commit="db7d45a191a7ee192775233682ff14938683e0c9"
source=("${_realname}"::"git+https://github.com/mccdo/osgworks.git#commit=${_commit}"
        "osg-3.3.x.patch"
        "osg-3.6.2.patch"
        "mingw-fpc-libs.patch")
sha256sums=('SKIP'
            '8a05e43a4947cb4dddb77ab8d20e806cb00abaa0ecfa677b3df5989562da5e4e'
            '220c5960e447e1ebc7208bea6e58a6211beca2f9e3bebea14719ec8721911193'
            '1de62ae24ee5dbe53ab51d9b4f9b362427b05f7e6a42083287388aad6cd785a1')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.%s" "${_ver_base}" "$(git rev-list --count "${_commit}")"
}

prepare() {
  cd "${srcdir}/${_realname}"
  patch -p1 -i ${srcdir}/osg-3.3.x.patch
  patch -p1 -i ${srcdir}/osg-3.6.2.patch
  patch -p1 -i ${srcdir}/mingw-fpc-libs.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    ../${_realname}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR=${pkgdir} "${MINGW_PREFIX}"/bin/cmake.exe --install .

  # support relocation
  local MINGW_PREFIX_W=$(cygpath -m "${MINGW_PREFIX}")
  sed -i "s#${MINGW_PREFIX_W}#\${_IMPORT_PREFIX}#g" "${pkgdir}${MINGW_PREFIX}"/lib/osgworks-targets-release.cmake
}
